package com.zoe.onelink.logging.trace.zipkin.sampler;

import brave.http.HttpRequest;
import brave.sampler.SamplerFunction;
import cn.hutool.core.lang.Assert;
import com.zoe.onelink.logging.trace.property.TraceLogProperties;
import com.zoe.onelink.util.PathMatchUtil;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-01-18
 */
public class ZipkinHttpSampler implements SamplerFunction<HttpRequest> {

    private final TraceLogProperties.HttpLogProperties httpLogProperties;

    public ZipkinHttpSampler(TraceLogProperties traceLogProperties) {
        this.httpLogProperties = traceLogProperties.getHttp();
        Assert.notNull(this.httpLogProperties);
    }


    @Override
    public Boolean trySample(HttpRequest request) {
        String url = request.path();
        return PathMatchUtil.isPermit(url, this.httpLogProperties.getOnlyUrls(), this.httpLogProperties.getExcludeUrls(),
                true, true, "ZIPKIN_SAMPLE");
    }
}
